package com.itcanteen.medical.patient.service.impl;


import com.itcanteen.medical.patient.entity.Image;
import com.itcanteen.medical.patient.entity.result;
import com.itcanteen.medical.patient.util.*;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class FaceMatchImpl  {


    /**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */

   /* public static void main(String[] args) {
        try {
            String s = FileUtil.readFileAsString("C:\\Users\\99774\\Pictures\\Camera Roll\\WIN_20191107_14_05_09_Pro.jpg");
            byte[] bytes = FileUtil.readFileByBytes(s);
            String encode = Base64Util.encode(bytes);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/


        /**
         * 重要提示代码中所需工具类
         * FileUtil,Base64Util,HttpUtil,GsonUtils请从
         * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
         * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
         * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
         * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
         * 下载
         */




        public static result faceMatch(Image image) {
            // 请求url
            String url = "https://aip.baidubce.com/rest/2.0/face/v3/match";
            try {

                byte[] data1 = FileUtil.readFileByBytes(image.getImage1());
                byte[] data2 = FileUtil.readFileByBytes(image.getImage2());

                HashMap<Object, Object> map1 = new HashMap<>();
                HashMap<Object, Object> map2 = new HashMap<>();

                map1.put("image", Base64Util.encode(data1));
                map1.put("image_type","BASE64");
                map2.put("image",Base64Util.encode(data2));
                map2.put("image_type","BASE64");

                Object[] parm = {map1,map2};


                String param = GsonUtils.toJson(parm);
                String auth = AuthService.getAuth();


                // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
                String accessToken = auth;

                result result =  HttpUtil.post(url, accessToken, "application/json", param);
                System.out.println(result);
                return result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }


        //public static void main(String[] args) {
         //   FaceMatchImpl.faceMatch(image1,image2);
       // }
    }


